package frobagroup.services.frontEnd.impl;

import frobagroup.entities.frontEnd.ShoppingCart;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.pagination.PageableImpl;
import frobagroup.pagination.Pagination;
import frobagroup.repositories.backstage.ProductRepository;
import frobagroup.repositories.frontEnd.ShoppingCartRepository;
import frobagroup.repositories.frontEnd.UserProfileRepository;
import frobagroup.services.frontEnd.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by Tinsley on 2018/5/22.
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartRepository shoppingCartRepository;
    @Autowired
    private UserProfileRepository userProfileRepository;
    @Autowired
    private ProductRepository productRepository;

    @Override
    public ShoppingCart addOrEditShoppingCart(ShoppingCart shoppingCart, String uId) {

        if (StringUtils.isEmpty(shoppingCart.getId())){
            ShoppingCart shoppingC = shoppingCartRepository.findByPIdAndUIdAndSizeAndProductColor(shoppingCart.getPId(),uId,
                    shoppingCart.getSize(),shoppingCart.getProductColor());
            if (!StringUtils.isEmpty(shoppingC)){
                shoppingC.setPurchaseNum(shoppingC.getPurchaseNum()+shoppingCart.getPurchaseNum());
                return shoppingCartRepository.save(shoppingC);
            }
            shoppingCart.setCommitTime(new Date());
            shoppingCart.setUId(uId);
            ShoppingCart sc = shoppingCartRepository.save(shoppingCart);
            UserProfile userProfile = userProfileRepository.findByUId(uId);
            userProfile.getScIds().add(sc.getId());
            userProfileRepository.save(userProfile);
            return sc;
        }else {
            shoppingCart.setUpdateTime(new Date());
            ShoppingCart sc = shoppingCartRepository.save(shoppingCart);
            return sc;
        }

    }

    @Override
    public boolean removeShoppingCart(List<String> scIds, String uId) {

        shoppingCartRepository.deleteByIdIn(scIds);
        UserProfile userProfile  = userProfileRepository.findByUId(uId);
        userProfile.getScIds().remove(scIds);
        userProfileRepository.save(userProfile);

        return Boolean.TRUE;
    }

    @Override
    public Pagination findShoppingCartList(Pagination pagination, String uId) {
        UserProfile userProfile = userProfileRepository.findByUId(uId);
        if (userProfile.getScIds().size()>0) {

            String sort = Objects.isNull(pagination.getSort()) ? "commitTime" : pagination.getSort();
            PageableImpl pageable = new PageableImpl(pagination.getPageNumber(), pagination.getPageSize(),
                    new Sort(Objects.equals(Sort.Direction.DESC.name(), pagination.getDir()) ? Sort.Direction.DESC : Sort.Direction.ASC, sort));
            Page<ShoppingCart> pages = shoppingCartRepository.findByIdIn(userProfile.getScIds(), pageable);
            pagination.setContent(pages.getContent());
            pagination.setTotal(pages.getTotalElements());
        }
        return pagination;
    }

    @Override
    public UserProfile weatherFollow(String pId, String uId, String weatherFollow) {

        UserProfile userProfile = userProfileRepository.findByUId(uId);
        if (Objects.equals(weatherFollow,"follow"))
            userProfile.getFollowProductIds().add(pId);
        else
            userProfile.getFollowProductIds().remove(pId);
        return userProfileRepository.save(userProfile);
    }
}
